﻿using com.nm.ApiServer;
using com.nm.ApiServer.Attributes;
using com.nm.Attributes;
using com.nm.db.Models.Terminal;
using com.nm.Helper;
using EntityFramework.Extensions;
using george.Data;
using george.Helper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TerminalAPI.models;

namespace TerminalAPI
{
    [Description("货架基本资料")]
    public class GoodShelfHandler : BaseHandler
    {
        [MustLogin]
        [Description("获取货架列表")]
        [ReturnType(typeof(PaperData<GoodData.ListGoodShelfData>))]
        public ApiResult List(
            [ParamDescription("搜索关键字")] string Keys = "",
            [ParamDescription("投入使用时间")] DateTime InjectDate = default(DateTime),
            [ParamDescription("停止使用时间")] DateTime StopDate = default(DateTime),
            [ParamDescription("删除标识")] int IsRemove = -1,
            [ParamDescription("页码")]int Page = 1,
            [ParamDescription("页数")]int Size = 10)
        {
            var dc = new TerminalEntities();
            var query = (from a in dc.Tm_GoodShelf
                         select new
                         {
                             a.CreateDate,
                             a.GSCode,
                             a.GSName,
                             a.Category,
                             a.InjectDate,
                             a.StopDate,
                             a.IsRemove,
                             a.GSID,
                             a.Generation
                         });
            #region 筛选条件
            if (!string.IsNullOrWhiteSpace(Keys))
            {
                query = query.Where(p => p.GSCode.Contains(Keys) || p.GSName.Contains(Keys));
            }
            if (IsRemove == 0)
            {
                query = query.Where(r => r.IsRemove == false);
            }
            else if (IsRemove == 1)
            {
                query = query.Where(r => r.IsRemove == true);
            }
            if (InjectDate != default(DateTime) || StopDate != default(DateTime))
            {
                if (InjectDate != default(DateTime))
                {
                    InjectDate = InjectDate.SetHour(0).SetMinute(0).SetSecond(0);
                }
                if (StopDate != default(DateTime))
                {
                    StopDate = StopDate.SetHour(23).SetMinute(59).SetSecond(59);
                }
                if (InjectDate != default(DateTime))
                    query = query.Where(o => o.InjectDate.Value >= InjectDate);
                if (StopDate != default(DateTime))
                    query = query.Where(o => o.StopDate.Value <= StopDate);
            }
            #endregion
            var result = PaperData<GoodData.ListGoodShelfData>.Create(query.Count(), Size, Page);
            result.Items = query.OrderByDescending(t => t.CreateDate)
                          .Skip(result.Skip)
                          .Take(result.Limt).ToList()
                          .Select(o => new GoodData.ListGoodShelfData
                          {
                              GSID=o.GSID,
                              GSCode = o.GSCode,
                              GSName = o.GSName,
                              Category = o.Category,
                              InjectDate = (DateTime)o.InjectDate,
                              StopDate = (DateTime)o.StopDate,
                              IsRemove = o.IsRemove,
                              CreateDate = (DateTime)o.CreateDate,
                              //Image=ImageHelper.GetResolveImageUrl(o.GSCode,"good","http"),
                              Generation=o.Generation
                          }).ToList();
            return Success(result);
        }

        [MustLogin]
        [Description("获取货架明细")]
        [ReturnType(typeof(GoodData.ListGoodShelfData))]
        public ApiResult Detail(
           [ParamDescription("ID")] int ID)
        {
            var db = new TerminalEntities();
            var query = db.Tm_GoodShelf.FirstOrDefault(s => s.GSID == ID);
            if (query == null) return Error("无数据");
            var retult = new GoodData.ListGoodShelfData
            {
                GSID=query.GSID,
                GSCode = query.GSCode,
                GSName = query.GSName,
                Category = query.Category,
                InjectDate = (DateTime)query.InjectDate,
                StopDate = (DateTime)query.StopDate,
                IsRemove = query.IsRemove,
                CreateDate = (DateTime)query.CreateDate,
                Image = ImageHelper.GetResolveImageUrl(query.GSCode,"good", "http"),
                Generation=query.Generation
            };
            return Success(retult);
        }

        [MustLogin]
        [Description("增加货架基本资料")]
        [ReturnType(typeof(int), "0-成功")]
        public ApiResult Create([ParamDescription("货架编号")] string Code, 
            [ParamDescription("货架名称")] string Name,
            [ParamDescription("类别")] string Category,
            [ParamDescription("代别")] string Generation="",
            [ParamDescription("投入使用时间")] DateTime InjectDate=default(DateTime),
            [ParamDescription("停止使用时间")] DateTime StopDate = default(DateTime))
        {
            if (string.IsNullOrWhiteSpace(Code)) return Error("货架编号不能为空");
            if (string.IsNullOrWhiteSpace(Name)) return Error("货架名称不能为空");
            if (string.IsNullOrWhiteSpace(Category)) return Error("类别不能为空");
            if (InjectDate==default(DateTime)) return Error("投入使用时间格式不正确");
            if (InjectDate == default(DateTime)) return Error("停止使用时间格式不正确");
            var dc = new TerminalEntities();
            Code = Code.StartTrim();
            if (dc.Tm_GoodShelf.Any(t => (t.GSCode == Code || t.GSName==Name )&& t.IsRemove == false)) return Error("货架编号或名称已存在");
            var good = new Tm_GoodShelf
            {
                Category = Category,
                CreateDate = DateTime.Now,
                GSCode = Code,
                GSName = Name,
                InjectDate = InjectDate,
                IsRemove = false,
                ModifyDate = DateTime.Now,
                Operator = this.UserId,
                StopDate = StopDate,
                Generation= Generation
            };
            dc.Tm_GoodShelf.Add(good);
            try
            {
                dc.SaveChanges();
            }
            catch(Exception ex)
            {
                return Error(ex.Message);
            }
            return Success(0);
        }
        
        [MustLogin]
        [Description("编辑货架基本资料")]
        [ReturnType(typeof(int), "0-成功")]
        public ApiResult Modify([ParamDescription("ID")] int ID,
            [ParamDescription("货架编号")] string Code,
            [ParamDescription("货架名称")] string Name,
            [ParamDescription("类别")] string Category,
            [ParamDescription("代别")] string Generation="",
            [ParamDescription("投入使用时间")] DateTime InjectDate = default(DateTime),
            [ParamDescription("停止使用时间")] DateTime StopDate = default(DateTime))
        {
            if (string.IsNullOrWhiteSpace(Code)) return Error("货架编号不能为空");
            if (string.IsNullOrWhiteSpace(Name)) return Error("货架名称不能为空");
            if (string.IsNullOrWhiteSpace(Category)) return Error("类别不能为空");
            if (InjectDate == default(DateTime)) return Error("投入使用时间格式不正确");
            if (InjectDate == default(DateTime)) return Error("停止使用时间格式不正确");
            var dc = new TerminalEntities();
            Code = Code.StartTrim();
           
            if (dc.Tm_GoodShelf.Any(t => t.GSCode == Code && t.GSID != ID))
                return Error($"货架名称【{Name}】已存在");
            var query = dc.Tm_GoodShelf.FirstOrDefault(t => t.GSID == ID);
            if (query != null)
            {
                query.Category = Category;
                query.GSCode = Code;
                query.GSName = Name;
                query.InjectDate = InjectDate;
                query.ModifyDate = DateTime.Now;
                query.Operator = this.UserId;
                query.StopDate = StopDate;
                query.Generation = Generation;
            }
            try
            {
                dc.SaveChanges();
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
            return Success(0);
        }

        [MustLogin]
        [Description("移除货架基本资料")]
        [ReturnType(typeof(int), "0-成功")]
        public ApiResult Remove([ParamDescription("ID")] int ID)
        {
            var dc = new TerminalEntities();
            var query = dc.Tm_GoodShelf.FirstOrDefault(t => t.GSID == ID);
            if (dc.Tm_ShelfDisplay.Any(t => t.GSID == ID)) return Error($"货架名称【{query?.GSName}】已被引用，无法删除!");
            if (query == null) return Error("无数据");
            query.IsRemove = true;
            try
            {
                dc.SaveChanges();
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
            return Success(0);
        }

    }
}
